package com.lx.javasunmmrize.proxy.dynamicProxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @Description TODO
 * @Author lx
 * @Date 2021/2/28 20:28
 **/
public class DyProxy {

    //传入一个目标对象
    Object teacherDao;

    //构造器,对target进行初始化
    public  DyProxy(Object teacherDao){
        this.teacherDao = teacherDao;
    }

    //给目标对象生成一个代理对象
    public  Object getProxyInstance(){
       return Proxy.newProxyInstance(teacherDao.getClass().getClassLoader(),
               teacherDao.getClass().getInterfaces(),
               new InvocationHandler() {
                   @Override
                   public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                       System.out.println("动态代理开始========");
                       Object res = method.invoke(teacherDao, args);
                       System.out.println("动态代理结束========");
                       return res;
                   }
               }
       );

    }
}
